function create_obj(){
	var td = navigator.appName;
	if(td == "Microsoft Internet Explorer"){
		obj = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		obj = new XMLHttpRequest();
	}
	return obj;
}
var http = create_obj();
function init(){
	rate_tour 	= document.getElementById("rate_tour").value;
	rate_adults = document.getElementById("rate_adults").value;
	rate_child 	= document.getElementById("rate_child").value;
	rate_ifants = document.getElementById("rate_ifants").value;
	startdate   = document.getElementById("startdate").value;
	//links       = "http://localhost:8080/";
}
function showrooms(links,tour,adults,child,infants,start,ship,type,empty){
	if(tour == 0){
		alert("Please select a program!");
		return false;
	}
	document.getElementById("shows").style.display = "block";
	document.getElementById("fromdate").innerHTML = start;
	//document.getElementById("change").disabled = false;
	document.getElementById("loader").innerHTML = "<img src='"+links+"public/images/ajax-loading.gif' />";
	http.open("POST",links+"home/shipajax/index/",true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	http.onreadystatechange = process;
	http.send("tour="+tour+"&adults="+adults+"&child="+child+"&infants="+infants+"&start="+start+"&ship="+ship+"&type="+type+"&empty="+empty);
}

function process(){
	if(http.readyState == 4 && http.status == 200){
		result = http.responseText;
		document.getElementById("loader").innerHTML = "";
		document.getElementById("show_room").innerHTML = result;
	}
}
function price($i){
	$totalp   = document.getElementById("totalp"+$i);
	$totalps  = document.getElementById("totalps"+$i);
	$singler  = document.getElementById("singler"+$i).value;
	$doubler  = document.getElementById("doubler"+$i).value;
	$twinr    = document.getElementById("twinr"+$i).value;
	$singler  = document.getElementById("singler"+$i).value;
	$adults	  = document.getElementById("adults"+$i).value;
	$child	  = document.getElementById("child"+$i).value;
	$infant	  = document.getElementById("infant"+$i).value;
	$vat      = document.getElementById("vat"+$i).value;
	$line     = document.getElementById("line"+$i);
	$ppri     = document.getElementById("p"+$i);
	$type     = document.getElementById("typerate").value;
	$price_promo_sing1 = document.getElementById("price_promo_1sing"+$i).getAttribute('name');
	$price_promo_sing2 = document.getElementById("price_promo_2sing"+$i).getAttribute('name');
	$price_promo_doubl1 = document.getElementById("price_promo_1double"+$i).getAttribute('name');
	$price_promo_doubl2 = document.getElementById("price_promo_2double"+$i).getAttribute('name');
	$price_single 	= document.getElementById("price_single"+$i).getAttribute('name');
	$price_double 	= document.getElementById("price_double"+$i).getAttribute('name');
	$price_twin     = $price_double;
	$price_child 	= document.getElementById("price_child"+$i).getAttribute('name');
	$price_infant 	= document.getElementById("price_infant"+$i).getAttribute('name');
    $price_singler  = $singler*$price_single;
	$price_doubler  = $doubler*$price_double;
	$price_twinr    = $twinr*$price_twin;
	$price_childr   = $child*$price_child;
	$price_infantr  = $infant*$price_infant;
	$price_rate_sing = $singler*$price_promo_sing1;
	$price_rate_doub = $doubler*$price_promo_doubl1;
	$price_rate_twin = $twinr*$price_promo_doubl1;
	$rate = parseInt($price_rate_sing) + parseInt($price_rate_doub) + parseInt($price_rate_twin) + parseInt($price_childr) + parseInt($price_infantr);
	$price_all = parseInt($price_singler) + parseInt($price_doubler) + parseInt($price_twinr) + parseInt($price_childr) + parseInt($price_infantr);
	if($vat == 1){
		$vat_rate  = ($rate*10)/100;
		$vat_price = ($price_all*10)/100;
	}else{
		$vat_rate  = 0;
		$vat_price = 0;
	}
	$price_all_total = $price_all +$vat_price;
	//alert($price_all + "  -  " +$vat_price+" - "+$price_all_total);
	$rate_all =  $rate + $vat_rate;
	if($type == "vnd"){
		//$line.innerHTML = Math.round($rate_all)+" VND";
		$totalp.innerHTML = Math.round($price_all)+" VND";
		$totalps.innerHTML = Math.round($price_all_total);
	}else{
		//$line.innerHTML = "$"+Math.round($rate_all);
		$totalp.innerHTML = "$"+Math.round($price_all);
		$totalps.innerHTML = Math.round($price_all_total);
	}
}
$(document).ready(function(){
	$("span.close").live("click",function(){
		$(this).parent().fadeOut("slow");
	});
	$("#visa").live("click",function(){
		$total  = $("#cart_total").text();
	    $show_price  = $("#cart_total");
		$first  = $("#firstprice");
		$visa = $("#visanum");
		$visanum = $visa.val();
		$typep = $("#typep").val();
		if($typep == "vnd"){
			$rate = " VND";
		}else{
			$rate = " USD";
		}
		if($(this).is(':checked')){
		  	$visa.removeAttr('disabled');
			if($typep == "vnd"){
				$total = parseFloat($total) + ($visanum*210000);
				$("#visaprice").html(($visanum*210000)+$rate);
			}else{
				$total = parseFloat($total) + ($visanum*10);
				$("#visaprice").html(($visanum*10)+$rate);
			}
		}else{
			$visa.attr('disabled', 'disabled');
			if($typep == "vnd"){
				$total = parseInt($total) - ($visanum*210000);
			}else{
				$total = parseInt($total) - ($visanum*10);
			}
			$("#visaprice").html("0"+$rate);
		}
		$("#totalprice").attr("value",$total);
		$show_price.html(Math.round($total)+$rate);
	});
	$("#visanum").live("change",function(){
		$show_price  = $("#cart_total");
		$first  = $("#firstprice").attr("name");
		var $selected = new Array();
			$('input.cartservice').each(function() {
				if($(this).is(':checked')){
		    		$selected.push($(this).attr('value'));
				}
		});
		var $total = 0;
		for(var $i = 0; $i < $selected.length; $i++) {
		    $total += $selected[$i] << 0;
		}
		$val    =  $(this).val();
		if($typep == "vnd"){
			$price  = parseFloat($total) + parseFloat($first) + parseInt($val*210000);   
			$("#visaprice").html(($val*210000)+$rate);
		}else{
			$price  = parseFloat($total) + parseFloat($first) + parseInt($val*10);   
			$("#visaprice").html(($val*10)+$rate);
		}
		$show_price.html(Math.round($price)+$rate);
		$("#totalprice").attr("value",Math.round($price));
	});
});
function shuttlebusprice(idadult,idchild){
	var r=0;
	$a=$("#"+idadult);
	$c=$("#"+idchild);
	if($a.length>0){
		r+=$a.attr("pri")*$a.val();
	}
	if($c.length>0){
		r+=$c.attr("pri")*$c.val();
	}
	return r;
}
function recalctotal(){
	var totalprice=parseFloat($("#firstprice").attr("name"));
	$(".totalcount").each(function(){
		totalprice+=parseFloat($(this).html().replace("$",""));
	});
	return totalprice;
}
$(document).ready(function(){
	$abc    = $(".cartservice");
	$show_price  = $("#cart_total");
	$visa     = $("#visa");  
	$num_visa = $("#visanum"); 
	$first    = $("#firstprice");
	$adults=$(".adulttransfer");
	$adults.live("change",function(){
		$total  = $("#cart_total").text(); 
		$checkbox=$("#service"+$(this).attr("rel"));
		$i=$(this).attr("rel");
		$typep = $("#typep").val();
		if($typep == "vnd"){
			$rate = " VND";
		}else{
			$rate = " USD";
		}
		//$price=parseInt($total) + parseInt($visa_price);
		if($checkbox.is(":checked")){
			var money=shuttlebusprice("adulttransfer"+$i,"childtransfer"+$i);
			if($("#adulttransfer"+$i).val()==0 || $("#adulttransfer"+$i).val()==1)
				$("#spad"+$i).html(" adult");
			else $("#spad"+$i).html(" adults");
			if($("#childtransfer"+$i).val()==0 || $("#childtransfer"+$i).val()==1)
				$("#spch"+$i).html(" child");
			else $("#spch"+$i).html(" children");
			$checkbox.val(money+$rate);
			$("#price_service"+$i).html(money+$rate);
		}
		$price=recalctotal();
		$show_price.html($price+$rate);
		$("#totalprice").attr("value",Math.round($price));
		//alert($checkbox.val());
	});
	$abc.live("click",function(){
		$i = $(this).attr("alt");
		$total  = $("#cart_total").text();
		$val   = $(this).attr("value");
		$typep = $("#typep").val();
		if($typep == "vnd"){
			$rate = " VND";
		}else{
			$rate = " USD";
		}
		if($(this).is(':checked')){
			if($("#adulttransfer"+$i).length>0){
				$val=shuttlebusprice("adulttransfer"+$i,"childtransfer"+$i);
				$("#adulttransfer"+$i).removeAttr('disabled');
				$("#childtransfer"+$i).removeAttr('disabled');
			}
			$(this).val($val);
			$("#price_service"+$i).html($val+$rate);
		}else{
			$("#adulttransfer"+$i).attr('disabled','disabled');
			$("#childtransfer"+$i).attr('disabled','disabled');
			$("#price_service"+$i).html("0"+$rate);
		} 
		$price=recalctotal();
		$show_price.html($price+$rate);
		$("#totalprice").attr("value",Math.round($price));
		//$first.attr("name",$first_price);
	});
});
$(document).ready(function(){
	var $items = $("a.arate");
	var $cart  = $("#cart");
	$items.live("click",function(){
		$i = $(this).attr("id");
		$startdate = $("#startdate").val();
		$endate    = $("#endate").text();
		$rate_tour = $("#rate_tour").val();
		$price  = $("#totalps"+$i).text();
		$prices = $("#totalp"+$i).text();
		$price = $price.substr(1);
		$avaible = $("#numavai"+$i).val();
		$singler = $("#singler"+$i).val();
		$doubler = $("#doubler"+$i).val();
		$twinr   = $("#twinr"+$i).val();
		$adults  = $("#adults"+$i).val();
		$child   = $("#child"+$i).val();
		$infant  = $("#infant"+$i).val();
		$ship    = $("#cruise_id").val();
		$vat     = $("#vat"+$i).val();
		$title   = $(".room_title"+$i).text();
		$idroom  = $(".idroom"+$i).text();
		$img     = $("#img_aj"+$i).val();
		$okie    = $(".okie"+$i);
		if($singler == 0){
			$max_single_adults = 0;
		}else{
			$max_single_adults = $singler*1;
		}
		$max_single_childs = 0;
		$max_single_infant = 0;
		$max_double_adults = $doubler*2;
		$max_double_childs = $doubler*1;
		$max_double_infant = $doubler*1;
		$max_twin_adults   = $twinr*2;
		$max_twin_childs   = $twinr*1;
		$max_twin_infant   = $twinr*1;
		$total_max_single  = parseInt($max_single_adults) + parseInt($max_single_childs) + parseInt($max_single_infant);
		$total_max_double  = parseInt($max_double_adults) + parseInt($max_double_childs) + parseInt($max_double_infant);
		$total_max_twin    = parseInt($max_twin_adults)   + parseInt($max_twin_childs)   + parseInt($max_twin_infant);
		$max_adults   = parseInt($max_single_adults) + parseInt($max_double_adults) + parseInt($max_twin_adults);
		$max_childs   = parseInt($max_single_childs) + parseInt($max_double_childs) + parseInt($max_twin_childs);
		$max_infant   = parseInt($max_single_infant) + parseInt($max_double_infant) + parseInt($max_twin_infant);
		$max_all      = parseInt($total_max_single) + parseInt($total_max_double) + parseInt($total_max_twin);
		$total_person = parseInt($adults) + parseInt($child) + parseInt($infant);
		$total_rooms  = parseInt($singler) + parseInt($doubler) + parseInt($twinr);
		if($price == "" || $price == 0 || $total_rooms == ""){
			alert("Please select cabin type!");
			return false;
		}else{
			if($total_rooms > $avaible){
				if($avaible == 1){ $roomalt = "room";}else{
					$roomalt = "rooms";
				}
				alert("Not enough room, Only "+$avaible+" "+$roomalt+" left");
				return false;
			}
			if($total_person > $max_all){
				if($singler != 0 && $doubler == 0 && $twinr == 0){
					alert("Single cabins can only accommodate 1 person");
				}else if($singler == 0 && $doubler != 0 || $twinr != 0){
				    alert("Cabins can accommodate a maximum of 2 adults, 1 child and 1 infant");
				}
				return false;
			}else{
				if($total_rooms > $adults){
					if($adults == 1){
						alert(" Maximum of "+$adults+" person per cabin!");
					}else{
						if($adults == 0){
							alert("Each cabin must have at least 1 adult!");
						}else{
							alert(" Maximum of "+$adults+" people in "+$adults+" cabins!");
						}
					}
					return false;
				}else{
					if($adults > $max_adults){
						alert("Maximum allowance of "+$max_adults+ " adults!");
						return false;
					}
					if($child > $max_childs){
						alert("Maximum allowance of "+$max_childs+ " children!");
						return false;
					}
					if($infant > $max_infant){
						alert("Maximum allowance of "+$max_infant+ " infants!");
						return false;
					}else{
						
						$.ajax({
							"url"  	: links+"home/cart/add",
							"type"	: "post",
							"data"  : "singler="+$singler+"&doubler="+$doubler+"&twinr="+$twinr+"&adults="+$adults+"&child="+$child+"&infants="+$infant+"&price="+$price+"&ship="+$ship+"&title="+$title+"&roomid="+$idroom+"&image="+$img+"&startdate="+$startdate+"&endate="+$endate+"&rate_tour="+$rate_tour+"&vat="+$vat+"&prices="+$prices,
							"async" : false,
							beforeSend : function(){
								$okie.html("<img src='"+links+"public/images/ajax-loader2.gif' />");
							},
							success : function(result){
								if(result == "false!"){
									$("#errors").show();
									$("#errors").html("You must complete booking for one cruise before booking for the next one, <a style='color:#00F' href='"+links+"your-cart#ship_info'>Click here</a> to see cruises you have already booked or <a style='color:#00F' href='"+links+"home/cart/emptycart'>Click here</a> to empty cart");
									//alert("only ship!");
									return false;
								}else{
									$okie.html("<span style='color:green;font-size:10px' class='removeok'>Ok</span>");
									$cart.load(links+'home/cart');
									$cart.fadeIn("slow");
								}
							}
						});
					}
				}
			}
		}
		return false;
	});
});
$(document).ready(function(){
	var $items = $("a.charters");
	var $cart  = $("#cart");
	$items.live("click",function(){
		$i = $(this).attr("id");
		$startdate = $("#startdate").val();
		$endate    = $("#endate").text();
		$rate_tour = $("#rate_tour").val();
		$price  = $(".abc"+$i).text();
		$prices = $price.substr(1);
		//$prices = $prices.substr(1);
		$extraprice = $("#extraprice1").val();
		$singler = "";
		$doubler = "";
		$twinr   = "";
		$adults  = 2;
		$child   = "";
		$infant  = "";
		$ship    = $("#cruise_id").val();
		$vat     = "";
		$title   = $(".room_title"+$i).text();
		$idroom  = $(".idroom"+$i).text();
		$img     = $("#img_aj"+$i).val();
		$okie    = $(".okie"+$i);
		$extra   = $("#extraperson"+$i).val();
		//alert($extra);
		//return false;
		$.ajax({
			"url"  	: links+"home/cart/add",
			"type"	: "post",
			"data"  : "singler="+$singler+"&doubler="+$doubler+"&twinr="+$twinr+"&adults="+$adults+"&child="+$child+"&infants="+$infant+"&prices="+$price+"&price="+$price+"&ship="+$ship+"&title="+$title+"&roomid="+$idroom+"&image="+$img+"&startdate="+$startdate+"&endate="+$endate+"&rate_tour="+$rate_tour+"&vat="+$vat+"&extra="+$extra+"&priceextra="+$extraprice,
			"async" : false,
			beforeSend : function(){
				$okie.html("<img src='"+links+"public/images/ajax-loader2.gif' />");
			},
			success : function(result){
				if(result == "false!"){
					$("#errors").show();
					$("#errors").html("You must complete booking for one cruise before booking for the next one, <a style='color:#00F' href='"+links+"your-cart#ship_info'>Click here</a> to see cruises you have already booked or <a style='color:#00F' href='"+links+"home/cart/emptycart'>Click here</a> to empty cart");
					//alert("only ship!");
					return false;
				}else{
					$okie.html("<span style='color:green;font-size:10px' class='removeok'>Ok</span>");
					$cart.load(links+'home/cart');
					$cart.fadeIn("slow");
				}
			}
		});
		return false;
	});
});
$(document).ready(function(){
	$items = $('a.continus');
	$items.live("click",function(){
		$('#formcart').submit();
	});
});
function showDetailImage(domimg,srcimg){
        if($("#abc").length==0){
            var divtooltip= $('<div id="abc" style="display:none; position:absolute; float:left;"></div>');
            $('body').append(divtooltip);
        }
        $("#abc").html('<img style="width: 300px;" src="'+srcimg+'"/>').show();
        var position=$("#abc").position();
        var windowscroll=$(window).scrollTop();
        $(domimg).mouseout(function() {
            $("#abc").hide();
        });
        $(domimg).mousemove(function(e) {
            var moveDown=20;
            var moveLeft=10;

            $("#abc").css('top', e.pageY + moveDown).css('left', e.pageX + moveLeft);
        });
}